package com.czwj.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.DataGridData;
import com.czwj.entities.FinishApply;
import com.czwj.entities.Form;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.ProjectItem;
import com.czwj.entities.UserInfo;
import com.czwj.service.FinishApplyService;
import com.czwj.service.FormService;
import com.czwj.service.ProjectItemService;
import com.czwj.util.TextUtil;
import com.czwj.util.WechatUtil;

import net.sf.json.JSONObject;

@Controller
public class FinishApplyAction{
	@Resource
	FinishApplyService finishApplyService;
	
	@Resource
	ProjectItemService projectItemService;
	
	@Resource
	FormService formService;
	
	@Value("${review_model_code}")
	private String model_code;
	
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
	
	@RequestMapping(value = "/finishApply.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<FinishApply> finishApply(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "status",required = false) String status,
			@RequestParam(value = "projectitemid",required = false) String projectitemid){
		DataGridData<FinishApply> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(status)){
			map.put("status",status);
		}
		if(TextUtil.isNotEmpty(projectitemid) && !projectitemid.equals("0")){
			map.put("projectitemid",projectitemid);
		}
		List<FinishApply> list = finishApplyService.searchbylevel3(map);
		result.setRows(list);
		result.setTotal(finishApplyService.countbylevel3(map));
		return result;
	}
	
	@SystemControllerLog(description = "修改竣工申请")
	@RequestMapping(value = "/finishApplySave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult finishApplySave(@RequestParam(value = "images",required = false) MultipartFile[] images,
			@RequestParam(value = "id",required = false) Integer id,
			@RequestParam(value = "projectitemid",required = true) Integer projectitemid,
			@RequestParam(value = "content",required = true) String content,
			@RequestParam(value = "status",required = true) Integer status,HttpServletRequest request) throws Exception{
		OperateResult result = new OperateResult();
		int count = 0;
		Subject currentUser = SecurityUtils.getSubject();
		int createrid = ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId();
		String createip = request.getRemoteAddr();
		count = finishApplyService.insertorupdate(id,projectitemid,content,status,createrid,createip,images);
		if(count > 0){
			ProjectItem projectItem = projectItemService.selectByPrimaryKey(projectitemid);
			for(UserInfo user:projectItem.getLevel2()){
				JSONObject data = WechatUtil.setReviewData(projectItem.getName() + " 预算执行",sdf2.format(new Date()));
				sendReview(user,data);
			}
		}
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "禁用竣工申请")
	@RequestMapping(value = "finishApplyDelete.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult deleteFinishApply(@RequestParam("id") int id){
		OperateResult result = new OperateResult();
		int count = finishApplyService.updateStatus(id);
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "删除竣工申请")
	@RequestMapping(value = "finishApplyRemove.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult removeFinishApply(@RequestParam("id") int id){
		OperateResult result = new OperateResult();
		int count = finishApplyService.remove(id);
		result.setSuccess(count > 0);
		return result;
	}
	
	@RequestMapping(value = "/finishApplyReviewed.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<FinishApply> finishApplyReviewed(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows){
		DataGridData<FinishApply> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid",userInfo.getId());
		if(userInfo.getRoleid() == 1 || userInfo.getRoleid() == 2){
			map.put("status","3,4");
			result.setRows(finishApplyService.searchReviewedLevel1(map));
			result.setTotal(finishApplyService.countReviewedLevel1(map));
		}else if(userInfo.getRoleid() == 3){
			map.put("status","3,4,7");
			result.setRows(finishApplyService.searchReviewedLevel2(map));
			result.setTotal(finishApplyService.countReviewedLevel2(map));
		}
		return result;
	}
	
	private void sendReview(UserInfo userInfo,JSONObject data){
		List<Form> forms = formService.findbyuser(userInfo.getId());
		for(Form form:forms){
			String code = WechatUtil.pushMessage(userInfo.getOpenid(),model_code,form.getFormid(),data);
			formService.deleteByPrimaryKey(form.getId());
			if(code.trim().equals("0")){
				return;
			}
		}
	}
}
